package com.itheima.demo05stream;

import org.junit.Test;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Demo06Collection {

    //其他收集流中数据方式(相当于数据库中的聚合函数)

    //拼接
    @Test
    public void testJoining(){
        Stream<Student> studentStream = Stream.of(new Student("赵丽颖", 52, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));
        String collect = studentStream.map(Student::getName)
                .collect(Collectors.joining("_"));
//        String collect = studentStream.map(Student::getName).collect(Collectors.joining(">_<", "^_^", "^v^"));
        System.out.println("collect = " + collect);


        
    }

    //分区
    @Test
    public void testPartition(){
        Stream<Student> studentStream = Stream.of(new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));
        Map<Boolean, List<Student>> map = studentStream.collect(Collectors.partitioningBy(s -> s.getScore() > 60));
        map.forEach((k,v)->{
            System.out.println(k+"::" + v);
        });
    }

    //多级分组
    @Test
    public void testCustomGroup() {
        Stream<Student> studentStream = Stream.of(new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("柳岩", 52, 77));
        Map<Integer, Map<Integer, List<Student>>> map = studentStream.collect(Collectors.groupingBy(Student::getAge, Collectors.groupingBy(Student::getScore)));
        map.forEach((k,v)->{
            System.out.println("k = " + k);
            v.forEach((k2,v2)->{
                System.out.println("k2 = " + v2);
            });
        });
    }

    //分组
    @Test
    public void testGroup() {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("刘岩", 52, 77));
//        Map<Integer, List<Student>> map = studentStream.collect(Collectors.groupingBy(s -> s.getAge()));
        Map<Integer, List<Student>> map = studentStream.collect(Collectors.groupingBy(Student::getAge));

        //将分数大于60的分为一组，小于60的分为一组

        map.forEach((k, v) -> {
            System.out.println("k = " + v);
        });

    }


    @Test
    public void testStreamToOther() {
        Stream<Student> studentStream = Stream.of(
                new Student("赵丽颖", 58, 95),
                new Student("杨颖", 56, 88),
                new Student("迪丽热巴", 56, 99),
                new Student("刘岩", 52, 77)
        );
        Optional<Student> max = studentStream.collect(
                Collectors.maxBy((s1, s2) -> s1.getScore() - s2.getScore())
        );
        System.out.println("最大值:" + max.get());

    }
}
